﻿using Grpc.Net.Client;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace CodeCity.K8S
{
    public static class ExtensionBuilder
    {
        public static IServiceCollection AddDockerService(this IServiceCollection services, string dockerServerUrl)
        {
            services.AddTransient(sp =>
            {
                var httpClientHandler = new HttpClientHandler();
                // Return `true` to allow certificates that are untrusted/invalid
                httpClientHandler.ServerCertificateCustomValidationCallback =
                    HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
                var httpClient = new HttpClient(httpClientHandler);

                var channel = GrpcChannel.ForAddress(dockerServerUrl,
                    new GrpcChannelOptions { HttpClient = httpClient });
                return new DockerServiceClient(channel);
            });
            return services;
        }
    }
}
